package com.sk.sourcecircle.activity.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.DetailsOfConsultationActivity;
import com.sk.sourcecircle.activity.home.activity.GroupDetailsActivity;
import com.sk.sourcecircle.adapter.GroupConsultAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.entity.NoticeInfoBean;
import com.sk.sourcecircle.utils.AppApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by sk on 2018/1/25.
 */

public class GroupConsultFragment extends BaseFragment implements HandleDataCallBack, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    Unbinder unbinder;
    private int id;
    private String name;

    private int page = 1;
    List<NoticeInfoBean.DataBean> allDataList = new ArrayList<>();
    private GroupConsultAdapter adapter;
    private boolean isLoadMore = false;

    @Override
    protected int getContentView() {
        return R.layout.layout_recycler;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getActivity() instanceof GroupDetailsActivity) {
            id = ((GroupDetailsActivity) getActivity()).getGroupId();
            name = ((GroupDetailsActivity) getActivity()).getName();
        }

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new GroupConsultAdapter(R.layout.item_local_article, allDataList, name);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);

        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);

        AppApi.getNotice(page, "0", null, String.valueOf(id), this, 0, getActivity());
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        NoticeInfoBean noticeInfoBean = JSON.parseObject(result, NoticeInfoBean.class);
        List<NoticeInfoBean.DataBean> dataBeanList = noticeInfoBean.getData();
        if (page == 1 && allDataList.size() > 0) {
            allDataList.clear();
        }
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, DetailsOfConsultationActivity.class);
        intent.putExtra("id", allDataList.get(position).getArticle_id());
        startActivity(intent);
        int show = allDataList.get(position).getShow();
        allDataList.get(position).setShow(show+1);
        adapter.notifyItemChanged(position);
    }

    @Override
    public void onLoadMoreRequested() {
        isLoadMore = true;
        page++;
        AppApi.getNotice(page, "0", null, String.valueOf(id), this, 0, getActivity());
    }

}
